package com.sftz.modules.minapp.coursestudy.service;

import java.util.Map;

import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.SqlManager;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import com.google.common.collect.Maps;
import com.sftz.assets.entities.CourseEntity;
import com.sftz.assets.entities.ResourceDataEntity;
import com.sftz.assets.entities.ResourceEntity;
import com.sftz.assets.enums.CourseResourceTypeEnum;
import com.sftz.assets.enums.CourseTypeEnum;
import com.sftz.assets.enums.DIYResourceTypeEnum;
import com.sftz.assets.enums.ResourceIsDeleteEnum;
import com.sftz.assets.enums.ResourceStatusEnum;
import com.uxuexi.core.common.util.EnumUtil;
import com.uxuexi.core.common.util.Util;
import com.uxuexi.core.db.dao.IDbDao;

/**
 * 课程大纲相关
 * @author   gyp
 * @Date	 2018年12月25日
 */
@IocBean
public class CourseOutlineService {

	@Inject
	protected IDbDao dbDao;
	@Inject
	protected Dao nutDao;
	@Inject
	protected SqlManager sqlManager;

	public Map<String, Object> courseRecourseOutLine(int courseid) {

		CourseEntity course = dbDao.fetch(CourseEntity.class, Cnd.where("id", "=", courseid));
		Integer courseType = course.getCourseType();
		if (!Util.isEmpty(course.getCourseType())) {
			CourseTypeEnum ct = EnumUtil.get(CourseTypeEnum.class, courseType);
			Map<String, Object> obj = Maps.newHashMap();
			switch (ct) {
			//个人课程
			case USERCOURSE:
				if (!Util.isEmpty(course)) {
					ResourceEntity resourceEntity = dbDao.fetch(
							ResourceEntity.class,
							Cnd.where("courseId", "=", course.getId())
									.and("resourcetype", "=", DIYResourceTypeEnum.OUTLINE.intKey())
									.and("status", "=", ResourceStatusEnum.START.intKey()));
					if (!Util.isEmpty(resourceEntity)) {
						ResourceDataEntity outLine = dbDao.fetch(
								ResourceDataEntity.class,
								Cnd.where("resourceid", "=", resourceEntity.getId())
										.and("resourceType", "=", CourseResourceTypeEnum.OUTLINE.intKey())
										.and("isDelete", "=", ResourceIsDeleteEnum.ENABLE.intKey()));
						if (!Util.isEmpty(outLine.getResourceContent())) {
							obj.put("outLine", outLine.getResourceContent());

						}
					}
				}
				return obj;
				//机构课程
			case INSTITUTIONCOURSE:
				if (!Util.isEmpty(course)) {
					ResourceEntity resourceEntity = dbDao.fetch(
							ResourceEntity.class,
							Cnd.where("courseId", "=", course.getId())
									.and("resourcetype", "=", DIYResourceTypeEnum.OUTLINE.intKey())
									.and("status", "=", ResourceStatusEnum.START.intKey()));
					if (!Util.isEmpty(resourceEntity)) {
						ResourceDataEntity outLine = dbDao.fetch(
								ResourceDataEntity.class,
								Cnd.where("resourceid", "=", resourceEntity.getId())
										.and("resourceType", "=", CourseResourceTypeEnum.OUTLINE.intKey())
										.and("isDelete", "=", ResourceIsDeleteEnum.ENABLE.intKey()));
						if (!Util.isEmpty(outLine)) {
							obj.put("outLine", outLine.getResourceContent());
						}
					}
				}
				return obj;
			default:
				break;
			}
		}

		return null;
	}
}
